package com.fire.eyes.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fire.core.base.controller.BaseController;
import com.fire.core.util.AjaxResult;
import com.fire.eyes.constants.CommonConstants;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.model.*;
import com.fire.eyes.service.IAssetCategoryService;
import com.fire.eyes.service.IAssetService;
import com.fire.eyes.validation.*;
import com.smartthing.rest.model.asset.LocationAsset;
import com.smartthing.rest.model.search.AssetSearchResults;
import com.smartthing.spi.SmartThingException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName: EyesController
 * @Description: TODO
 * @Author raoyuxing
 * @Date 2019/1/7 16:30
 */
@Controller
public class AssetController extends BaseController {
    private final static Logger blogin = Logger.getLogger(AssetController.class);

    @Autowired
    private IAssetCategoryService assetCategoryService;
    @Autowired
    private IAssetService assetService;

    /**
     * 查询消防资产链表
     *
     * @return
     * @author raoyuxing
     */
    @PostMapping(value = "/assetlist")
    @ResponseBody
    public AjaxResult assetlist(@RequestBody @Validated({TokenDTO.paging.class, SiteIdDTO.Isassigned.class})
                                            SiteIdDTO siteIdDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                blogin.error(fieldError.getDefaultMessage());
            }
            return ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "失败", "失败");
        }
        Map<String, Object> map = new HashMap<>();
        try {
            if (CommonConstants.YES.equals(siteIdDTO.getIsassigned())) {
                List results = assetService.getAssetListBySiteId(siteIdDTO);
                map.put("assetlists", results);
            }else {
                List results = assetService.getAssetList(siteIdDTO);
                map.put("assetlists", results);
            }
        } catch (SmartThingException e) {
            e.printStackTrace();
            return ajaxResult(ErrorCodeConstants.INTERNAL_ERROR , "失败", e , "失败");
        }
        return ajaxResult(ErrorCodeConstants.SUCCESS, "成功", map, "成功");
    }

    /**
     * 查询消防资产详情
     *
     * @return
     * @author raoyuxing
     */
    @PostMapping(value = "/assetdetails")
    @ResponseBody
    public AjaxResult assetdetails(@RequestBody @Validated AssetIdDTO assetIdDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                blogin.error(fieldError.getDefaultMessage());
            }
            return ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "失败", "失败");
        }
        try {
            LocationAsset result = assetService.getAssetDetail(assetIdDTO);
            return ajaxResult(ErrorCodeConstants.SUCCESS, "成功", result, "成功");
        } catch (SmartThingException e) {
            e.printStackTrace();
            return ajaxResult(ErrorCodeConstants.INTERNAL_ERROR , "失败", e , "失败");
        }
    }

    /**
     * 查询所有资产品类
     *
     * @return
     * @author raoyuxing
     */
    @PostMapping(value = "/assetcategory")
    @ResponseBody
    public AjaxResult assetcategory(@RequestBody @Validated({TokenDTO.paging.class}) MonitorDTO monitorDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                blogin.error(fieldError.getDefaultMessage());
            }
            return ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "失败", "失败");
        }

        Page<AssetCategoryModel> page = new Page(monitorDTO.getPage(),monitorDTO.getPagesize());
        Wrapper<AssetCategoryModel> wrapper = new EntityWrapper<>();
        Page<AssetCategoryModel> assetCategorys = assetCategoryService.selectPage(page,wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("assetcategorylist", assetCategorys.getRecords());

        return ajaxResult(ErrorCodeConstants.SUCCESS, "成功", map, "成功");
    }

    /**
     * 增加资产接口
     *
     * @return
     * @author raoyuxing
     */
    @PostMapping(value = "/addasset")
    @ResponseBody
    public AjaxResult addasset(@RequestBody @Validated AddAssetDTO addAssetDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                blogin.error(fieldError.getDefaultMessage());
            }
            return ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "失败", "失败");
        }

        try {
            LocationAsset result = assetService.addAsset(addAssetDTO);
            return ajaxResult(ErrorCodeConstants.SUCCESS, "成功", result, "成功");
        } catch (SmartThingException e) {
            e.printStackTrace();
            return ajaxResult(ErrorCodeConstants.INTERNAL_ERROR , "失败", e , "失败");
        }
    }

}
